package com.evil.common.core.enums;

import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Optional;

/**
 * 开关值
 */
@AllArgsConstructor
public enum SwitchEnum {

    /**
     * 开关值
     */
    NO(0, "关", "假"),
    YES(1, "开", "真"),
    ;

    @Getter
    private final int id;
    @Getter
    private final String name;
    @Getter
    private final String desc;

    public static Optional<SwitchEnum> isExistById(Integer id) {
        if (null == id) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static SwitchEnum findById(Integer id) {
        return SwitchEnum.isExistById(id).orElseThrow(() -> new BusinessException(RCodeEnum.ERROR_PARAMETER, "应用类型参数错误"));
    }

    public static SwitchEnum findByBoolean(boolean sign) {
        return sign ? YES : NO;
    }

    public static boolean isOpen(Integer id) {
        return SwitchEnum.isExistById(id).map(e -> YES.id == e.id).orElse(false);
    }

    public static boolean isClose(Integer id) {
        return SwitchEnum.isExistById(id).map(e -> NO.id == e.id).orElse(false);
    }
}
